package org.kunteng.yunac.lttask.controller.view;

import org.kunteng.yunac.lttask.AbstractTask;
import org.kunteng.yunac.lttask.LTTaskManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by hedingwei on 6/5/15.
 */
@Controller
@RequestMapping(value = "${adminPath}/f/lttask")
public class LogView {

    private ModelAndView init = new ModelAndView("f/lttask/log");
    @Autowired
    private LTTaskManager longTimeTaskRunner;

    @RequestMapping(value = "log.view.do")
    public ModelAndView action(HttpServletRequest request,@RequestParam("taskId")String taskId,@RequestParam("logId")int logId) {

        if(longTimeTaskRunner.isRegisteredTask(taskId)){
            AbstractTask abstractTask = longTimeTaskRunner.getHistoryTaskLog(logId);
            if(abstractTask==null){
                init.addObject("hasHistoryTask",false);
            }else{
                init.addObject("hasHistoryTask",true);
                init.addObject("task_model",abstractTask.getModel());
            }
            init.addObject("isRunning", false);
            init.addObject("isUnRegisteredTask",false);
            return init;
        }else{
            init.addObject("isUnRegisteredTask",true);
            return init;
        }
    }
}
